<!-- eslint-disable no-undef -->
<script setup lang="ts">
import { reqUpload } from '@/services/consult'
import type { Picture } from '@/types/consult'
import type { UploaderAfterRead } from 'vant/lib/uploader/types'

defineProps<{
  disabled: boolean
}>()
// 1. 输入框绑定值
// 2. 按下回车发出消息 通知父组件 父组件接收输入的值
// 3. 父组件接收子传父的自定义事件后
// 3. socket.emit('senChatMsg', {from, to, msgType , msg: {content}} )
const emits = defineEmits<{
  (e: 'sendText', v: string): void
  (e: 'sendImage', v: Picture): void
}>()
// 输入的值
const text = ref('')
// 子传父传递输入的信息
const sendText = () => {
  if (text.value.trim()) {
    emits('sendText', text.value)
    text.value = ''
  }
}
const afterRead: UploaderAfterRead = async (item) => {
  if (Array.isArray(item)) return
  if (!item.file) return
  const res = await reqUpload(item.file)
  // console.log(res.data)
  emits('sendImage', res.data)
}
</script>
<template>
  <div class="room-action">
    <van-field
      type="text"
      class="input"
      :border="false"
      placeholder="问医生"
      autocomplete="off"
      :disabled="disabled"
      v-model="text"
      @keyup.enter="sendText"
    ></van-field>
    <van-uploader
      :preview-image="false"
      :disabled="disabled"
      :after-read="afterRead"
    >
      <cp-icon name="consult-img" />
    </van-uploader>
  </div>
</template>

<style lang="scss" scoped>
.room-action {
  position: fixed;
  left: 0;
  bottom: 0;
  width: 100%;
  height: 60px;
  background-color: #fff;
  display: flex;
  align-items: center;
  padding: 0 16px;
  z-index: 1;
  box-sizing: border-box;
  .input {
    background-color: var(--cp-bg);
    border: none;
    border-radius: 25px;
    margin-right: 10px;
    padding: 8px 15px;
  }
  .cp-icon {
    width: 24px;
    height: 24px;
  }
}
</style>
